# -*- coding: utf-8 -*-
# @Time        :2025/4/17 10:01
# @Author      :文刀水寿
# @File        : app.py
"""
 @Description :
"""
from fastapi import FastAPI, Request
import uvicorn
from Apps.app1.urls import shop
from fastapi.responses import Response


app = FastAPI()

app.include_router(shop, prefix="/middle", tags=["中间件"])


@app.middleware("http")
async def middle2(request: Request, call_next):
    # 请求代码块
    print("middle2 request")
    response = await call_next(request)

    # 响应代码块
    print("middle2 response")

    return response


@app.middleware("http")
async def middle(request: Request, call_next):
    # 请求代码块
    print("middle1 request")

    # ip拦截访问
    # if request.client.host in ["127.0.0.1"]:  # 黑名单
    #     return Response(status_code=404,
    #                     content="vivist dorbbinde")
    # 路径拦截访问
    if request.url.path in ["/middle/user"]:  # 黑名单
        return Response(status_code=404,
                        content="vivist dorbbinde")

    response = await call_next(request)

    # 响应代码块
    print("middle1 response")

    return response

# middle1 request
# middle2 request
# {'user': 'current user'}
# middle2 response
# middle1 response



if __name__ == '__main__':
    uvicorn.run("app:app",
                reload=True,
                port=8080)